package com.hbnu.zhanglian.dao;

import com.hbnu.zhanglian.entity.User;
import java.sql.*;

public class UserDAO {

    // 用户注册
    public boolean register(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();

            // 先检查用户名是否已存在
            String checkSql = "SELECT id FROM users WHERE username = ?";
            pstmt = conn.prepareStatement(checkSql);
            pstmt.setString(1, user.getUsername());
            rs = pstmt.executeQuery();

            if (rs.next()) {
                System.out.println("用户名已存在: " + user.getUsername());
                return false; // 用户名已存在
            }

            // 关闭之前的ResultSet和Statement
            rs.close();
            pstmt.close();

            // 插入新用户
            String insertSql = "INSERT INTO users(username, password) VALUES(?, ?)";
            pstmt = conn.prepareStatement(insertSql);
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());

            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("用户注册成功: " + user.getUsername());
                return true;
            }
            return false;

        } catch (SQLException e) {
            System.err.println("用户注册失败: " + e.getMessage());
            return false;
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
    }

    // 用户登录验证
    public User login(String username, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setScore(rs.getInt("score"));
                System.out.println("用户登录成功: " + username);
                return user;
            } else {
                System.out.println("登录失败: 用户名或密码错误");
                return null;
            }

        } catch (SQLException e) {
            System.err.println("登录查询失败: " + e.getMessage());
            return null;
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
    }

    // 更新用户积分
    public boolean updateScore(int userId, int newScore) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE users SET score = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, newScore);
            pstmt.setInt(2, userId);

            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            System.err.println("更新积分失败: " + e.getMessage());
            return false;
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
    }
}